/*
 *Copyright 2021-2023 NERCIS
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
 */

package cn.ac.nercis.pes.service.project;

import cn.ac.nercis.pes.common.utils.JsonUtils;
import cn.ac.nercis.pes.model.dto.common.Result;
import cn.ac.nercis.pes.model.dto.project.ImportConflictDTO;
import cn.ac.nercis.pes.model.standard.*;
import cn.ac.nercis.pes.model.standard.analyze.*;
import cn.ac.nercis.pes.model.standard.asset.AssetSDTO;
import cn.ac.nercis.pes.model.standard.asset.PersonnelSDTO;
import cn.ac.nercis.pes.model.standard.asset.SecurityDocumentSDTO;
import cn.ac.nercis.pes.model.standard.evaluation.EvaluationRecordSDTO;
import cn.ac.nercis.pes.model.standard.planning.EvaluationDescSDTO;
import cn.ac.nercis.pes.model.standard.planning.EvaluationMethodSDTO;
import cn.ac.nercis.pes.model.standard.planning.EvaluationRequireSDTO;
import cn.ac.nercis.pes.model.standard.prepare.*;
import cn.ac.nercis.pes.model.standard.proof.EvaluatorQualificationSDTO;
import cn.ac.nercis.pes.model.standard.report.EvaluationReportSDTO;
import cn.ac.nercis.pes.service.project.imported.*;
import cn.ac.nercis.pes.service.utils.AssetUtils;
import cn.ac.nercis.pes.service.utils.DistinctUtils;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * 系统数据导入服务
 *
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
@Service
public class SystemImportService {

    private final SystemBaseImportService systemBaseImportService;
    private final SystemTargetImportService systemTargetImportService;
    private final SystemSpecialTargetImportService systemSpecialTargetImportService;
    private final SystemFileImportService systemFileImportService;
    private final SystemProcessImportService systemProcessImportService;
    private final EvaluationProcessImportService evaluationProcessImportService;
    private final EvaluationContentImportService evaluationContentImportService;
    private final EvaluatorImportService evaluatorImportService;
    private final EvaluationToolsImportService evaluationToolsImportService;
    private final PasswordServerImportService passwordServerImportService;
    private final SecurityThreatImportService securityThreatImportService;
    private final PersonnelImportService personnelImportService;
    private final SecurityDocumentImportService securityDocumentImportService;
    private final SystemAssetImportService systemAssetImportService;
    private final EvaluationDescImportService evaluationDescImportService;
    private final EvaluationMethodImportService evaluationMethodImportService;
    private final EvaluationRequireImportService evaluationRequireImportService;
    private final EvaluationRecordImportService evaluationRecordImportService;
    private final UnitEvaluationImportService unitEvaluationImportService;
    private final ResultCorrectionImportService resultCorrectionImportService;
    private final RiskAnalysisImportService riskAnalysisImportService;
    private final EvaluationReportImportService evaluationReportImportService;
    private final EvaluatorQualificationImportService evaluatorQualificationImportService;


    private final List<ImportEntityData> importSystemData = new ArrayList<>();
    private final List<ImportEntityFile> importSystemFiles = new ArrayList<>();
    private final List<AssetSDTO> importAssetData = new ArrayList<>();

    @Autowired
    public SystemImportService(SystemBaseImportService systemBaseImportService,
                               SystemTargetImportService systemTargetImportService,
                               SystemSpecialTargetImportService systemSpecialTargetImportService,
                               SystemFileImportService systemFileImportService,
                               SystemProcessImportService systemProcessImportService,
                               EvaluationProcessImportService evaluationProcessImportService,
                               EvaluationContentImportService evaluationContentImportService,
                               EvaluatorImportService evaluatorImportService,
                               EvaluationToolsImportService evaluationToolsImportService,
                               PasswordServerImportService passwordServerImportService,
                               SecurityThreatImportService securityThreatImportService,
                               PersonnelImportService personnelImportService,
                               SecurityDocumentImportService securityDocumentImportService,
                               SystemAssetImportService systemAssetImportService,
                               EvaluationDescImportService evaluationDescImportService,
                               EvaluationMethodImportService evaluationMethodImportService,
                               EvaluationRequireImportService evaluationRequireImportService,
                               EvaluationRecordImportService evaluationRecordImportService,
                               UnitEvaluationImportService unitEvaluationImportService,
                               ResultCorrectionImportService resultCorrectionImportService,
                               RiskAnalysisImportService riskAnalysisImportService,
                               EvaluationReportImportService evaluationReportImportService,
                               EvaluatorQualificationImportService evaluatorQualificationImportService) {
        this.systemBaseImportService = systemBaseImportService;
        this.systemTargetImportService = systemTargetImportService;
        this.systemSpecialTargetImportService = systemSpecialTargetImportService;
        this.systemFileImportService = systemFileImportService;
        this.systemProcessImportService = systemProcessImportService;
        this.evaluationProcessImportService = evaluationProcessImportService;
        this.evaluationContentImportService = evaluationContentImportService;
        this.evaluatorImportService = evaluatorImportService;
        this.evaluationToolsImportService = evaluationToolsImportService;
        this.passwordServerImportService = passwordServerImportService;
        this.securityThreatImportService = securityThreatImportService;
        this.personnelImportService = personnelImportService;
        this.securityDocumentImportService = securityDocumentImportService;
        this.systemAssetImportService = systemAssetImportService;
        this.evaluationDescImportService = evaluationDescImportService;
        this.evaluationMethodImportService = evaluationMethodImportService;
        this.evaluationRequireImportService = evaluationRequireImportService;
        this.evaluationRecordImportService = evaluationRecordImportService;
        this.unitEvaluationImportService = unitEvaluationImportService;
        this.resultCorrectionImportService = resultCorrectionImportService;
        this.riskAnalysisImportService = riskAnalysisImportService;
        this.evaluationReportImportService = evaluationReportImportService;
        this.evaluatorQualificationImportService = evaluatorQualificationImportService;
    }

    public Result<SystemSDTO> loadImportSystemData(@NonNull InputStream inputStream) {
        try {
            this.clearData();
            this.readSystemData(inputStream);
            return importSystemData.stream()
                    .filter(s -> StringUtils.equals(s.name, StandardInfo.SYSTEM_BASE_FILE.getValue()))
                    .map(s -> getSystemBase(s.getContent()))
                    .filter(Objects::nonNull)
                    .map(Result::success)
                    .findFirst()
                    .orElse(Result.failed("未读取到导入的系统信息"));
        } catch (IOException ex) {
            log.error("检查导入的系统数据失败：", ex);
            return Result.failed("验证导入的系统数据失败！");
        }
    }

    public void clearData(){
        this.importSystemData.clear();
        this.importSystemFiles.clear();
        this.importAssetData.clear();
    }

    public Result<List<ImportConflictDTO>> checkSystemBaseData(@NonNull String selectSystemId) {
        Optional<SystemSDTO> importSystem = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SYSTEM_BASE_FILE.getValue()))
                .map(s -> getSystemBase(s.getContent()))
                .filter(Objects::nonNull)
                .findFirst();
        if (importSystem.isEmpty()) {
            return Result.failed("未读取到导入的系统信息");
        }
        if(Objects.isNull(importSystem.get().getEvaluation())){
            return Result.failed("导入的被测信息系统基本信息不完整");
        }
        return systemBaseImportService.checkSystemBaseData(importSystem.get(),selectSystemId);
    }

    public Result<String> importSystemBaseData(String selectSystemId, @NonNull Boolean cover) {
        var importSystem = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SYSTEM_BASE_FILE.getValue()))
                .map(s -> getSystemBase(s.getContent()))
                .filter(Objects::nonNull)
                .findFirst()
                .orElse(null);
        if (Objects.isNull(importSystem)) {
            return Result.failed("未读取到导入的系统信息");
        }
        return systemBaseImportService.importSystemBaseData(importSystem, selectSystemId, cover);
    }

    public Result<List<ImportConflictDTO>> checkSystemTargetData(@NonNull String systemId) {
        List<SystemTargetSDTO> targets = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SYSTEM_TARGET_FILE.getValue()))
                .map(s -> parseSystemTargets(s.getContent()))
                .findFirst()
                .orElse(null);
        if (CollectionUtils.isEmpty(targets)) {
            return Result.failed("未读取到导入的指标信息");
        }
        return systemTargetImportService.checkSystemTargetData(systemId, targets);
    }

    public Result<String> importSystemTargetData(@NonNull String systemId, @NonNull Boolean cover) {
        List<SystemTargetSDTO> targets = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SYSTEM_TARGET_FILE.getValue()))
                .map(s -> parseSystemTargets(s.getContent()))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(targets)) {
            return Result.failed("未读取到导入的指标信息");
        }
        return systemTargetImportService.importSystemTargetData(systemId, targets, cover);
    }

    public Result<List<ImportConflictDTO>> checkSpecialTargetData(@NonNull String systemId) {
        List<SystemSpecialTargetSDTO> targets = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SYSTEM_SPECIAL_TARGET_FILE.getValue()))
                .map(s -> parseSpecialTargets(s.getContent()))
                .findFirst()
                .orElse(null);
        if (CollectionUtils.isEmpty(targets)) {
            return Result.success(Collections.emptyList());
        }
        return systemSpecialTargetImportService.checkSpecialTargetData(systemId, targets);
    }

    public Result<String> importSpecialTargetData(@NonNull String systemId, @NonNull Boolean cover) {
        List<SystemSpecialTargetSDTO> targets = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SYSTEM_SPECIAL_TARGET_FILE.getValue()))
                .map(s -> parseSpecialTargets(s.getContent()))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(targets)) {
            return Result.success();
        }
        return systemSpecialTargetImportService.importSpecialTargetData(systemId, targets, cover);
    }

    public Result<List<ImportConflictDTO>> checkSystemFiles(@NonNull String systemId) {
        if (CollectionUtils.isEmpty(importSystemFiles)) {
            return Result.success(Collections.emptyList());
        }
        return systemFileImportService.checkSystemFiles(systemId, importSystemFiles);
    }

    public Result<String> importSystemFiles(@NonNull String systemId, @NonNull Boolean cover) {
        if (CollectionUtils.isEmpty(importSystemFiles)) {
            return Result.success();
        }
        return systemFileImportService.importSystemFiles(systemId, importSystemFiles, cover);
    }

    public Result<List<ImportConflictDTO>> checkSystemProcessData() {
        return systemProcessImportService.checkSystemProcess();
    }

    public Result<String> importSystemProcessData(@NonNull String systemId, @NonNull Boolean cover) {
        List<SystemProcessSDTO> process = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SYSTEM_PROCESS_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), SystemProcessSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(process)) {
            return Result.success();
        }
        return systemProcessImportService.importSystemProcess(systemId, process.stream()
                .filter(DistinctUtils.customSystemProcessDistinct()).toList(), cover);
    }

    public Result<List<ImportConflictDTO>> checkEvaluationProcessData(@NonNull String systemId) {
        List<EvaluationProcessSDTO> evaluationProcess = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_PROCESS_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationProcessSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(evaluationProcess)) {
            return Result.success();
        }
        return evaluationProcessImportService.checkEvaluationProcessData(systemId, evaluationProcess.stream()
                .filter(DistinctUtils.customEvaluationProcessDistinct()).toList());
    }

    public Result<String> importEvaluationProcessData(@NonNull String systemId, @NonNull Boolean cover) {
        List<EvaluationProcessSDTO> evaluationProcess = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_PROCESS_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationProcessSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(evaluationProcess)) {
            return Result.success();
        }
        return evaluationProcessImportService.importEvaluationProcessData(systemId, evaluationProcess.stream()
                .filter(DistinctUtils.customEvaluationProcessDistinct()).toList(), cover);
    }

    public Result<List<ImportConflictDTO>> checkEvaluationContentData(@NonNull String systemId) {
        List<EvaluationContentSDTO> evaluationContents = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_CONTENT_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationContentSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(evaluationContents)) {
            return Result.success();
        }
        return evaluationContentImportService.checkEvaluationContentData(systemId, evaluationContents.stream()
                .filter(DistinctUtils.customEvaluationContentDistinct()).toList());
    }

    public Result<String> importEvaluationContentData(@NonNull String systemId, @NonNull Boolean cover) {
        List<EvaluationContentSDTO> evaluationContents = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_CONTENT_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationContentSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(evaluationContents)) {
            return Result.success();
        }
        return evaluationContentImportService.importEvaluationContentData(systemId, evaluationContents.stream()
                .filter(DistinctUtils.customEvaluationContentDistinct()).toList(), cover);
    }

    public Result<List<ImportConflictDTO>> checkEvaluatorData(@NonNull String systemId) {
        List<EvaluatorSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATOR_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluatorSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluatorImportService.checkEvaluatorData(systemId, importData);
    }

    public Result<String> importEvaluatorData(@NonNull String systemId, @NonNull Boolean cover) {
        List<EvaluatorSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATOR_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluatorSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluatorImportService.importEvaluatorData(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkEvaluationToolsData(@NonNull String systemId) {
        List<EvaluationToolSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_TOOLS_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationToolSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluationToolsImportService.checkEvaluationTools(systemId, importData);
    }

    public Result<String> importEvaluationToolsData(@NonNull String systemId, @NonNull Boolean cover) {
        List<EvaluationToolSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_TOOLS_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationToolSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluationToolsImportService.importEvaluationTools(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkPasswordServerData(@NonNull String systemId) {
        List<PasswordServerSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.PASSWORD_SERVER_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), PasswordServerSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return passwordServerImportService.checkPasswordServer(systemId, importData);
    }

    public Result<String> importPasswordServerData(@NonNull String systemId, @NonNull Boolean cover) {
        List<PasswordServerSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.PASSWORD_SERVER_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), PasswordServerSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return passwordServerImportService.importPasswordServer(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkSecurityThreatData(@NonNull String systemId) {
        List<SecurityThreatSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SECURITY_THREAT_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), SecurityThreatSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return securityThreatImportService.checkSecurityThreat(systemId, importData);
    }

    public Result<String> importSecurityThreatData(@NonNull String systemId, @NonNull Boolean cover) {
        List<SecurityThreatSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SECURITY_THREAT_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), SecurityThreatSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return securityThreatImportService.importSecurityThreat(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkPersonnelData(@NonNull String systemId) {
        List<PersonnelSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.PERSONNEL_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), PersonnelSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return personnelImportService.checkPersonnel(systemId, importData);
    }

    public Result<String> importPersonnelData(@NonNull String systemId, @NonNull Boolean cover) {
        List<PersonnelSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.PERSONNEL_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), PersonnelSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return personnelImportService.importPersonnel(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkSecurityDocumentData(@NonNull String systemId) {
        List<SecurityDocumentSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SECURITY_DOCUMENT_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), SecurityDocumentSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return securityDocumentImportService.checkSecurityDocument(systemId, importData);
    }

    public Result<String> importSecurityDocumentData(@NonNull String systemId, @NonNull Boolean cover) {
        List<SecurityDocumentSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.SECURITY_DOCUMENT_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), SecurityDocumentSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return securityDocumentImportService.importSecurityDocument(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkAssetData(@NonNull String systemId) {
        List<AssetSDTO> importData = findImportAssetData();
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        AssetUtils.repairImportAssetData(importData);
        return systemAssetImportService.checkAssetData(systemId, importData);
    }

    public Result<String> importAssetData(@NonNull String systemId, @NonNull Boolean cover) {
        List<AssetSDTO> importData = findImportAssetData();
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return systemAssetImportService.importAssetData(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkEvaluationMethodData(@NonNull String systemId) {
        List<AssetSDTO> importAssetData = findImportAssetData();
        List<EvaluationMethodSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_METHOD_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationMethodSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importAssetData) || CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        var evalDesc = checkEvaluationDescData(systemId,importAssetData);
        var evalMethod = evaluationMethodImportService.checkEvaluationMethod(systemId, importAssetData, importData.stream()
                .filter(DistinctUtils.customEvaluationMethodDistinct()).toList());
        if(CollectionUtils.isNotEmpty(evalDesc.getData()) && CollectionUtils.isNotEmpty(evalMethod.getData())){
            return Result.success(Stream.concat(evalDesc.getData().stream(),evalMethod.getData().stream()).toList());
        }else if(CollectionUtils.isNotEmpty(evalDesc.getData())){
            return evalDesc;
        }else{
            return evalMethod;
        }
    }

    private Result<List<ImportConflictDTO>> checkEvaluationDescData(@NonNull String systemId,List<AssetSDTO> importAssetData) {
        var importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_DESC_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationDescSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importAssetData) || CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluationDescImportService.checkEvaluationDesc(systemId, importAssetData, importData.stream()
                .filter(DistinctUtils.customEvaluationDescDistinct()).toList());
    }

    public Result<String> importEvaluationMethodData(@NonNull String systemId, @NonNull Boolean cover) {
        List<AssetSDTO> importAssetData = findImportAssetData();
        List<EvaluationMethodSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_METHOD_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationMethodSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importAssetData) || CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        var evalDescResult = importEvaluationDescData(systemId,cover,importAssetData);
        if(!evalDescResult.isSuccess()){
            return evalDescResult;
        }
        return evaluationMethodImportService.importEvaluationMethod(systemId, importAssetData, importData.stream()
                .filter(DistinctUtils.customEvaluationMethodDistinct()).toList(), cover);
    }

    private Result<String> importEvaluationDescData(@NonNull String systemId, @NonNull Boolean cover,List<AssetSDTO> importAssetData) {
        var importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_DESC_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationDescSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importAssetData) || CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluationDescImportService.importEvaluationDesc(systemId, importAssetData, importData.stream()
                .filter(DistinctUtils.customEvaluationDescDistinct()).toList(), cover);
    }

    public Result<List<ImportConflictDTO>> checkEvaluationRequireData(@NonNull String systemId) {
        List<EvaluationRequireSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_REQUIRE_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationRequireSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluationRequireImportService.checkEvaluationRequire(systemId, importData);
    }

    public Result<String> importEvaluationRequireData(@NonNull String systemId, @NonNull Boolean cover) {
        List<EvaluationRequireSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_REQUIRE_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationRequireSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluationRequireImportService.importEvaluationRequire(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkEvaluationRecordData(@NonNull String systemId) {
        List<EvaluationRecordSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_RECORD_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationRecordSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        List<AssetSDTO> importAssetData = findImportAssetData();
        if (CollectionUtils.isEmpty(importData) || CollectionUtils.isEmpty(importAssetData)) {
            return Result.success();
        }
        return evaluationRecordImportService.checkEvaluationRecord(systemId, importData.stream()
                .filter(DistinctUtils.customEvaluationRecordDistinct()).toList(), importAssetData);
    }

    public Result<String> importEvaluationRecordData(@NonNull String systemId, @NonNull Boolean cover) {
        List<EvaluationRecordSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_RECORD_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationRecordSDTO.class))
                .findFirst().orElse(null);
        List<AssetSDTO> importAssetData = findImportAssetData();
        if (CollectionUtils.isEmpty(importData) || CollectionUtils.isEmpty(importAssetData)) {
            return Result.success();
        }
        return evaluationRecordImportService.importEvaluationRecord(systemId, importData.stream()
                .filter(DistinctUtils.customEvaluationRecordDistinct()).toList(), importAssetData, cover);
    }

    public Result<List<ImportConflictDTO>> checkUnitEvaluationData(@NonNull String systemId) {
        return unitEvaluationImportService.checkUnitEvaluation(systemId,
                this.findImportUnitEvaluation(),
                this.findImportLayerEvaluation(),
                this.findImportOverallEvaluation());
    }

    public Result<String> importUnitEvaluationData(@NonNull String systemId, @NonNull Boolean cover) {
        return unitEvaluationImportService.importUnitEvaluation(systemId,
                this.findImportUnitEvaluation(),
                this.findImportLayerEvaluation(),
                this.findImportOverallEvaluation(),
                cover);
    }

    public Result<List<ImportConflictDTO>> checkResultCorrectionData(@NonNull String systemId) {
        List<ResultCorrectionSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.RESULT_CORRECTION_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), ResultCorrectionSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        List<AssetSDTO> importAssetData = this.findImportAssetData();
        List<EvaluationRecordSDTO> importRecordData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_RECORD_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationRecordSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData) || CollectionUtils.isEmpty(importAssetData) || CollectionUtils.isEmpty(importRecordData)) {
            return Result.success();
        }
        return resultCorrectionImportService.checkResultCorrection(systemId, importData.stream()
                .filter(DistinctUtils.customResultCorrectionDistinct()).toList(), importRecordData.stream()
                .filter(DistinctUtils.customEvaluationRecordDistinct()).toList(), importAssetData);
    }

    public Result<String> importResultCorrectionData(@NonNull String systemId, @NonNull Boolean cover) {
        List<ResultCorrectionSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.RESULT_CORRECTION_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), ResultCorrectionSDTO.class))
                .findFirst().orElse(null);
        List<AssetSDTO> importAssetData = this.findImportAssetData();
        List<EvaluationRecordSDTO> importRecordData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_RECORD_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationRecordSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData) || CollectionUtils.isEmpty(importAssetData) || CollectionUtils.isEmpty(importRecordData)) {
            return Result.success();
        }
        return resultCorrectionImportService.importResultCorrection(systemId, importData.stream()
                .filter(DistinctUtils.customResultCorrectionDistinct()).toList(), importRecordData.stream()
                .filter(DistinctUtils.customEvaluationRecordDistinct()).toList(), importAssetData, cover);
    }


    public Result<List<ImportConflictDTO>> checkRiskAnalysisData(@NonNull String systemId) {
        var importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.UNIT_RISK_ANALYSIS_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), UnitRiskAnalysisSDTO.class))
                .findFirst()
                .orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return riskAnalysisImportService.checkRiskAnalysis(systemId, importData.stream()
                .filter(DistinctUtils.customUnitRiskAnalysisDistinct()).toList());
    }

    public Result<String> importRiskAnalysisData(@NonNull String systemId, @NonNull Boolean cover) {
        var importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.UNIT_RISK_ANALYSIS_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), UnitRiskAnalysisSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return riskAnalysisImportService.importRiskAnalysis(systemId, importData.stream()
                .filter(DistinctUtils.customUnitRiskAnalysisDistinct()).toList(), cover);
    }

    public Result<List<ImportConflictDTO>> checkEvaluatorQualificationData(@NonNull String systemId) {
        var importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.PROOF_EVALUATOR.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluatorQualificationSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluatorQualificationImportService.checkEvaluatorQualificationData(systemId, importData);
    }

    public Result<String> importEvaluatorQualificationData(@NonNull String systemId, @NonNull Boolean cover) {
        var importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.PROOF_EVALUATOR.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluatorQualificationSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluatorQualificationImportService.importEvaluatorQualificationData(systemId, importData, cover);
    }

    public Result<List<ImportConflictDTO>> checkReportData(@NonNull String systemId) {
        List<EvaluationReportSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_REPORT_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationReportSDTO.class))
                .findFirst().orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluationReportImportService.checkEvaluationReport(systemId, importData.stream()
                .filter(DistinctUtils.customEvaluationReportDistinct()).toList());
    }

    public Result<String> importReportData(@NonNull String systemId, @NonNull Boolean cover) {
        List<EvaluationReportSDTO> importData = importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.EVALUATION_REPORT_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), EvaluationReportSDTO.class))
                .findFirst().orElse(null);
        if (CollectionUtils.isEmpty(importData)) {
            return Result.success();
        }
        return evaluationReportImportService.importEvaluationReport(systemId, importData.stream()
                .filter(DistinctUtils.customEvaluationReportDistinct()).toList(), cover);
    }

    private List<UnitEvaluationSDTO> findImportUnitEvaluation() {
        return importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.UNIT_EVALUATION_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), UnitEvaluationSDTO.class))
                .findFirst().orElse(Collections.emptyList());
    }

    private List<LayerEvaluationSDTO> findImportLayerEvaluation() {
        return importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.LAYER_EVALUATION_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), LayerEvaluationSDTO.class))
                .findFirst().orElse(Collections.emptyList());
    }

    private List<OverallEvaluationSDTO> findImportOverallEvaluation() {
        return importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.OVERALL_EVALUATION_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), OverallEvaluationSDTO.class))
                .findFirst().orElse(Collections.emptyList());
    }

    private List<AssetSDTO> findImportAssetData() {
        if(CollectionUtils.isNotEmpty(importAssetData)){
            return importAssetData;
        }
        var importAssets= importSystemData.stream()
                .filter(s -> StringUtils.equals(s.name, StandardInfo.ASSET_FILE.getValue()))
                .map(s -> JsonUtils.jsonToList(s.getContent(), AssetSDTO.class))
                .findFirst()
                .orElse(Collections.emptyList());
        if(CollectionUtils.isNotEmpty(importAssets)){
            importAssetData.addAll(importAssets.stream().filter(DistinctUtils.customAssetDistinct()).toList());
        }
        return importAssetData;
    }

    private void readSystemData(InputStream inputStream) throws IOException {
        try (ZipInputStream zis = new ZipInputStream(inputStream)) {
            ZipEntry entry = zis.getNextEntry();
            while (Objects.nonNull(entry)) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(bos);
                byte[] bytes = new byte[1024];
                int len;
                while ((len = zis.read(bytes)) != -1) {
                    bufferedOutputStream.write(bytes, 0, len);
                }
                bufferedOutputStream.close();
                if (StringUtils.startsWith(entry.getName(), StandardInfo.SYSTEM_FILE_DIR.getValue())) {
                    ImportEntityFile itemFile = new ImportEntityFile();
                    itemFile.setName(entry.getName());
                    itemFile.setData(bos.toByteArray());
                    importSystemFiles.add(itemFile);
                } else {
                    ImportEntityData itemData = new ImportEntityData();
                    itemData.setName(entry.getName());
                    itemData.setContent(StrUtil.str(bos.toByteArray(), StandardCharsets.UTF_8));
                    importSystemData.add(itemData);
                }
                zis.closeEntry();
                entry = zis.getNextEntry();
            }
        } catch (IOException ex) {
            log.error("读取导入的系统数据失败：", ex);
            throw ex;
        }
    }

    private SystemSDTO getSystemBase(@NonNull String content) {
        return JsonUtils.fromJson(content, SystemSDTO.class);
    }

    private List<SystemTargetSDTO> parseSystemTargets(@NonNull String content) {
        return JsonUtils.jsonToList(content, SystemTargetSDTO.class);
    }

    private List<SystemSpecialTargetSDTO> parseSpecialTargets(@NonNull String content) {
        return JsonUtils.jsonToList(content, SystemSpecialTargetSDTO.class);
    }


    @Data
    private static final class ImportEntityData {
        private String name;
        private String content;
    }

    @Data
    public static final class ImportEntityFile {
        private String name;
        private byte[] data;
    }
}